package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.*;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.schadmin.SchDetailVo;
import com.xjc.business.entity.vo.stu.AllSchoolVo;
import com.xjc.business.entity.vo.sysadmin.SchoolDetailVo;
import com.xjc.business.entity.vo.sysadmin.SchoolPageVo;
import com.xjc.business.entity.vo.sysadmin.SchoolVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.SchoolsMapper;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【schools(学校基本信息表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:32
*/
@Service
public class SchoolsServiceImpl extends ServiceImpl<SchoolsMapper, Schools>
    implements SchoolsService {

    @Lazy
    @Autowired
    SchoolDetailsService schoolDetailsService;

    @Lazy
    @Autowired
    SchoolApplysService schoolApplysService;

    @Autowired
    @Lazy
    SchoolCollegeRelationsService schoolCollegeRelationsService;

    @Autowired
    @Lazy
    CollegesService collegesService;

    @Autowired
    @Lazy
    CollageMajorRelationsService collageMajorRelationsService;

    @Autowired
    @Lazy
    MajorsService majorsService;

    @Autowired
    @Lazy
    MajorClazzRelationsService majorClazzRelationsService;;

    @Autowired
    @Lazy
    ClazzsService clazzsService;

    /**
     * 分页查询学校
     *
     * @param pageSize    每页条数
     * @param currentPage 当前页码
     * @param schoolName  学校名称
     * @return 学校数据
     */
    @Override
    public Result<PageVo<SchoolPageVo>> getSchoolPage(Integer pageSize, Integer currentPage, String schoolName) {

        LambdaQueryWrapper<Schools> schoolsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolsLambdaQueryWrapper.like(!schoolName.isEmpty(), Schools::getSchoolName, schoolName);

        Page<Schools> schoolsPage = new Page<>(currentPage, pageSize);

        page(schoolsPage, schoolsLambdaQueryWrapper);

        List<Schools> schoolsPageRecords = schoolsPage.getRecords();

        List<SchoolPageVo> schoolPageVoList = schoolsPageRecords.stream().map(schools -> {
            SchoolPageVo schoolPageVo = new SchoolPageVo();
            schoolPageVo.setSchoolName(schools.getSchoolName());
            schoolPageVo.setId(schools.getId());
            schoolPageVo.setPic(schools.getPic());
            schoolPageVo.setCreateTime(schools.getCreateTime());
            return schoolPageVo;
        }).collect(Collectors.toList());

        PageVo<SchoolPageVo> pageVo = new PageVo<>();

        pageVo.setData(schoolPageVoList);
        pageVo.setTotal(schoolsPage.getTotal());
        pageVo.setCurrent(currentPage);
        pageVo.setSize(pageSize);
        pageVo.setPageCount(schoolsPage.getPages());

        return Result.okWithData(pageVo);
    }

    /**
     * 获取学校详情
     * @param id 学校id
     * @return 详情
     */
    @Override
    public Result<SchoolDetailVo> getSchoolDetail(Integer id) {
        LambdaQueryWrapper<SchoolDetails> schoolDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolDetailsLambdaQueryWrapper.eq(SchoolDetails::getSchId, id);
        SchoolDetails schoolFromDB = schoolDetailsService.getOne(schoolDetailsLambdaQueryWrapper);
        SchoolDetailVo schoolDetailVo = new SchoolDetailVo();
        if (schoolFromDB != null) {
            schoolDetailVo.setCode(schoolFromDB.getCode());
            schoolDetailVo.setProvince(schoolFromDB.getProvince());
            schoolDetailVo.setCity(schoolFromDB.getCity());
            schoolDetailVo.setCounty(schoolFromDB.getArea());
            schoolDetailVo.setArea(schoolFromDB.getArea());
            schoolDetailVo.setDetails(schoolFromDB.getDetails());
            schoolDetailVo.setEmail(schoolFromDB.getEmail());
            schoolDetailVo.setPhone(schoolFromDB.getPhone());
            schoolDetailVo.setLevel(MyConstants.SCHOOL_LEVEL.get(schoolFromDB.getLevel()));
            schoolDetailVo.setLevel2((MyConstants.SCHOOL_LEVEL2.get(schoolFromDB.getLevel2())));
            schoolDetailVo.setCreateTime(schoolFromDB.getCreateTime());
        }
        return Result.okWithData(schoolDetailVo);
    }

    /**
     * 获取学校详情
     *
     * @param id 学校id
     * @return 学校详情
     */
    @Override
    public Result<SchDetailVo> getSchoolInfo(Integer id) {

        SchDetailVo schDetailVo = new SchDetailVo();

        Schools schoolFromDB = getById(id);
        if(schoolFromDB !=null){
            schDetailVo.setId(schoolFromDB.getId());
            schDetailVo.setSchoolName(schoolFromDB.getSchoolName());
            schDetailVo.setPic(schoolFromDB.getPic());
            schDetailVo.setCertificatePic(schoolFromDB.getCertificatePic());

            LambdaQueryWrapper<SchoolDetails> schoolDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            schoolDetailsLambdaQueryWrapper.eq(SchoolDetails::getSchId, id);
            SchoolDetails schoolDetailsFromDB = schoolDetailsService.getOne(schoolDetailsLambdaQueryWrapper);

            if (schoolDetailsFromDB != null) {
                schDetailVo.setCode(schoolDetailsFromDB.getCode());
                schDetailVo.setProvince(schoolDetailsFromDB.getProvince());
                schDetailVo.setCity(schoolDetailsFromDB.getCity());
                schDetailVo.setArea(schoolDetailsFromDB.getArea());
                schDetailVo.setDetails(schoolDetailsFromDB.getDetails());
                schDetailVo.setEmail(schoolDetailsFromDB.getEmail());
                schDetailVo.setPhone(schoolDetailsFromDB.getPhone());
                schDetailVo.setLevel((schoolDetailsFromDB.getLevel()));
                schDetailVo.setLevel2((schoolDetailsFromDB.getLevel2()));

                LambdaQueryWrapper<SchoolApplys> schoolApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
                schoolApplysLambdaQueryWrapper.eq(SchoolApplys::getSchoolName, schoolFromDB.getSchoolName());

                Integer status = schoolApplysService.getOne(schoolApplysLambdaQueryWrapper).getStatus();
                schDetailVo.setStatus(status);
            }
        }

        return Result.okWithData(schDetailVo);
    }

    /**
     * 获取学校列表
     *
     * @return 学校列表
     */
    @Override
    public Result<List<SchoolVo>> getSchoolList() {
        List<Schools> schoolsList = list();
        List<SchoolVo> schoolVoList = schoolsList.stream().map(schools -> {
            SchoolVo schoolVo = new SchoolVo();
            schoolVo.setId(schools.getId());
            schoolVo.setSchoolName(schools.getSchoolName());
            return schoolVo;
        }).collect(Collectors.toList());

        return Result.okWithData(schoolVoList);
    }

    /**
     * 获取学校列表
     *
     * @return 数据
     */
    @Override
    public Result<List<AllSchoolVo>> getAllSchoolList() {

        List<Schools> schoolsList = list();
        List<AllSchoolVo> allSchoolVoList = schoolsList.stream().map(schools -> {
            // 学校信息
            AllSchoolVo allSchoolVo = new AllSchoolVo();
            allSchoolVo.setSchoolId(schools.getId());
            allSchoolVo.setSchoolName(schools.getSchoolName());

            // 学校下属的学院信息
            LambdaQueryWrapper<SchoolCollegeRelations> schoolCollegeRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            schoolCollegeRelationsLambdaQueryWrapper.eq(SchoolCollegeRelations::getSchId,schools.getId());
            List<Integer> collegeIdList = schoolCollegeRelationsService.list(schoolCollegeRelationsLambdaQueryWrapper).stream().map(SchoolCollegeRelations::getColId).collect(Collectors.toList());
            if(collegeIdList.isEmpty()){

                allSchoolVo.setCollegeVoList(new ArrayList<>());
            }else{
                List<AllSchoolVo.CollegeVo> collegeVoList = collegesService.listByIds(collegeIdList).stream().map(college -> {

                    AllSchoolVo.CollegeVo collegeVo = new AllSchoolVo.CollegeVo();
                    collegeVo.setCollegeId(college.getId());
                    collegeVo.setCollegeName(college.getName());

                    // 学院下属专业信息
                    LambdaQueryWrapper<CollageMajorRelations> collegeMajorRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    collegeMajorRelationsLambdaQueryWrapper.eq(CollageMajorRelations::getColId,college.getId());
                    List<Integer> majorIdList = collageMajorRelationsService.list(collegeMajorRelationsLambdaQueryWrapper).stream().map(CollageMajorRelations::getMajId).collect(Collectors.toList());

                    if(majorIdList.isEmpty()){

                        collegeVo.setMajorVoList(new ArrayList<>());
                    }else{
                        List<AllSchoolVo.MajorVo> majorVoList = majorsService.listByIds(majorIdList).stream().map(major -> {
                            AllSchoolVo.MajorVo majorVo = new AllSchoolVo.MajorVo();
                            majorVo.setMajorId(major.getId());
                            majorVo.setMajorName(major.getName());

                            // 专业下属班级信息
                            LambdaQueryWrapper<MajorClazzRelations> majorClazzRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                            majorClazzRelationsLambdaQueryWrapper.eq(MajorClazzRelations::getMajId,major.getId());
                            List<Integer> classIdList = majorClazzRelationsService.list(majorClazzRelationsLambdaQueryWrapper).stream().map(MajorClazzRelations::getClId).collect(Collectors.toList());
                            if(classIdList.isEmpty()){

                                majorVo.setClassVoList(new ArrayList<>());
                            }else{
                                List<AllSchoolVo.ClassVo> classVoList = clazzsService.listByIds(classIdList).stream().map(clazz -> {
                                    AllSchoolVo.ClassVo classVo = new AllSchoolVo.ClassVo();
                                    classVo.setClassId(clazz.getId());
                                    classVo.setClassName(clazz.getNum()+"");
                                    return classVo;
                                }).collect(Collectors.toList());

                                majorVo.setClassVoList(classVoList);
                            }

                            return majorVo;
                        }).collect(Collectors.toList());

                        collegeVo.setMajorVoList(majorVoList);
                    }

                    return collegeVo;
                }).collect(Collectors.toList());

                allSchoolVo.setCollegeVoList(collegeVoList);
            }

            return allSchoolVo;
        }).collect(Collectors.toList());

        return Result.okWithData(allSchoolVoList);
    }
}




